package com.bdc.whatwhat.task;

import android.os.AsyncTask;

import com.bdc.whatwhat.Service.Service;
import com.bdc.whatwhat.modele.Whatter;

import java.util.List;

/**
 * Created by florentchampigny on 25/06/14.
 */
public class AjouterAmisTask extends AsyncTask<Object, Void, Object> {

    public interface AjouterAmisTaskTaskDelegate {
        public void afficherChargement(boolean afficher);

        public void onAmisAjouter();

        public void onErreurReseau();
    }

    AjouterAmisTaskTaskDelegate delegate;
    boolean erreurReseau;
    boolean resultatVide;

    Boolean accept = true;

    public AjouterAmisTask(AjouterAmisTaskTaskDelegate delegate) {
        this.delegate = delegate;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (delegate != null)
            delegate.afficherChargement(true);
    }

    @Override
    protected Object doInBackground(Object... objects) {
        String token = (String) objects[0];
        String id = (String) objects[1];

        if(objects.length>2)
            accept = (Boolean) objects[2];

        try {
            if(accept)
                Service.ajouterAmi(token,id);
            else
                Service.refuserAmi(token,id);
        } catch (Service.NetworkException e) {
            erreurReseau = true;
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Object accept) {

        if (delegate != null) {
            delegate.afficherChargement(false);
            if (erreurReseau)
                delegate.onErreurReseau();
            else
                delegate.onAmisAjouter();
        }
    }
}
